package com.spark.lola.controller.logistics;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.spark.lola.logistics.entity.ExpensesInfo;
import com.spark.lola.logistics.request.ExpensesInfoAddRequest;
import com.spark.lola.logistics.request.ExpensesInfoUpdateRequest;
import com.spark.lola.logistics.service.ExpensesInfoService;
import com.spark.lola.support.api.entity.EnterpriseTokenUser;
import com.spark.lola.support.common.entity.ShiroUser;
import com.spark.lola.support.common.util.ResponseBean;
import com.spark.lola.support.mybatis.common.QueryCondition;
import com.spark.lola.support.mybatis.entity.QueryParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

/**
 * @author: Zhi-peng wang
 * @date: 2020/05/25 14:36
 **/
@RestController
@RequestMapping(value = "/api/logistics/expenses", produces = "application/json;charset=utf-8")
@Api(value = "/api/logistics/expenses", tags = "货运支出单接口")
public class ExpensesController {

    @Autowired
    private ExpensesInfoService expensesInfoService;

    @ApiOperation(value = "分页查询货运支出单")
    @RequestMapping(value = "/findBy", method = RequestMethod.POST)
    public ResponseBean findPage(@RequestBody QueryParam<ExpensesInfo> queryParam, @SessionAttribute EnterpriseTokenUser<?> tokenUser) {
        queryParam.setQueryList(new ArrayList<>());
        QueryCondition queryCondition = new QueryCondition();
        queryCondition.setKey("o.status");
        queryCondition.setOper(">=");
        queryCondition.setValue(2);
        queryParam.getQueryList().add(queryCondition);
//        if (CollectionUtils.isEmpty(queryParam.getQueryList())) {
//            queryParam.setQueryList(new ArrayList<>());
//        }
        QueryCondition queryCondition1 = new QueryCondition();
        queryCondition1.setKey("o.empId");
        queryCondition1.setOper("=");
        queryCondition1.setValue(tokenUser.getShiroUser().getId());
        queryParam.getQueryList().add(queryCondition1);
        IPage<ExpensesInfo> expensesInfoIPage = expensesInfoService.findPage(queryParam);
        return ResponseBean.success(expensesInfoIPage);
    }

    @ApiOperation(value = "获取货运支出单详情")
    @ApiImplicitParam(name = "expensesId", value = "货运支出单id", required = true, dataType = "long", paramType = "path")
    @RequestMapping(value = "/findInfo/{expensesId}", method = RequestMethod.POST)
    public ResponseBean findInfo(@PathVariable long expensesId) {
        ExpensesInfo expensesInfo = new ExpensesInfo();
        if (expensesId != 0) {
            expensesInfo = expensesInfoService.findInfo(expensesId);
        }
        return ResponseBean.success(expensesInfo);
    }

    @ApiOperation(value = "创建货运支出单")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @Transactional(rollbackFor = Exception.class)
    public ResponseBean addExpenses(@RequestBody ExpensesInfoAddRequest expensesInfoAddRequest, @SessionAttribute EnterpriseTokenUser<?> tokenUser) throws Exception {
        ExpensesInfo model = new ExpensesInfo();
        BeanUtils.copyProperties(expensesInfoAddRequest, model);
        model.setCreator(tokenUser.getShiroUser().getRealName());
        model.setUpdator(tokenUser.getShiroUser().getRealName());
        model = expensesInfoService.addExpenses(model, tokenUser.getShiroUser());
        return ResponseBean.success(model);
    }

    @ApiOperation(value = "货运支出单确认")
    @ApiImplicitParam(name = "expensesId", value = "货运支出单信息id", required = true, dataType = "long", paramType = "query")
    @RequestMapping(value = "/complete", method = RequestMethod.POST)
    @Transactional(rollbackFor = Exception.class)
    public ResponseBean complete(@RequestBody JSONObject jsonObject, @SessionAttribute EnterpriseTokenUser<?> tokenUser) throws Exception {
        Long expensesId = jsonObject.getLong("expensesId");
        String evidence = jsonObject.getString("evidence");
        if (expensesId == null) {
            return ResponseBean.error("参数为空");
        }
        expensesInfoService.complete(expensesId, evidence, tokenUser.getShiroUser());
        return ResponseBean.success();
    }

//    @ApiOperation(value = "修改货运支出单")
//    @RequestMapping(value = "/save", method = RequestMethod.POST)
//    @Transactional(rollbackFor = Exception.class)
//    public ResponseBean saveExpenses(@RequestBody ExpensesInfoUpdateRequest expensesInfoUpdateRequest, @SessionAttribute EnterpriseTokenUser<?> tokenUser) throws Exception {
//        ExpensesInfo model = new ExpensesInfo();
//        BeanUtils.copyProperties(expensesInfoUpdateRequest, model);
//        model = expensesInfoService.updateExpenses(model, tokenUser.getShiroUser());
//        return ResponseBean.success(model);
//    }

    @ApiOperation(value = "删除货运支出单")
    @ApiImplicitParam(name = "nos", value = "删除货运支出单id集合", required = true, dataType = "long", paramType = "path")
    @RequestMapping(value = "/del/{expensesId}", method = RequestMethod.POST)
    @Transactional(rollbackFor = Exception.class)
    public ResponseBean del(@PathVariable long expensesId, @SessionAttribute EnterpriseTokenUser<?> tokenUser) throws Exception {
        expensesInfoService.deleteOrder(expensesId, tokenUser.getShiroUser());
        return ResponseBean.success();
    }

}
